package com.itjiange.controller;

import com.itjiange.service.UserService;
import com.itjiange.service.impl.UserServiceImpl;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 查询登录接口
 *
 * @author ItJiange
 * @date 2023/10/16
 */
@WebServlet("/user/login")
public class UserLoginController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String verCode = req.getParameter("verCode");
        // 验证码不正确
        if (!CaptchaUtil.ver(verCode, req)) {
            CaptchaUtil.clear(req);
            System.out.println("验证码不正确");
            resp.sendRedirect(req.getContextPath() + "/login");
            return;
        }
        // 2. 调用登录方法
        boolean flag = userService.userLogin(username, password);
        if (!flag) {
            System.out.println("登录失败");
            resp.sendRedirect(req.getContextPath() + "/register");
            return;
        }
        System.out.println("登陆成功");
        resp.sendRedirect(req.getContextPath() + "/user/selectAll");
    }
}
